package jvDeformation.gui;


import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import jv.number.PuDouble;
import jv.object.PsUpdateIf;

public class AlphaControler{

	
	final private PuDouble alphaValue;
	private boolean line = false;
	
	public boolean isLine(){
		return line;
	}
	
	public Panel getPanel(){
		return alphaValue.getInfoPanel();
	}
	
	public void setLineStatus(boolean status){
		line = status;
		if (status == true) {
			alphaValue.setBounds(0, 2, 1, 1);
			alphaValue.setValue(2);
		} else if (status == false) {
			alphaValue.setBounds(0, 10, 0.01, 0.01);
			alphaValue.setValue(1);
		}
		
		actionEvent("changed");
	}
	
	public double getValue(){
		if (line) {
			double v = alphaValue.getValue();
			if (v == 0 || v==1 || v==2) {
				return v;
			}else{
				alphaValue.setValue(2);
				return 2;
			}
			
		} else{
			return alphaValue.getValue();
		}
	}
	
	public AlphaControler() {
		this.alphaValue = new PuDouble("alpha");
		alphaValue.addUpdateListener(new PsUpdateIf() {
			PsUpdateIf parent;
			@Override
			public boolean update(Object event) {
				if (alphaValue == event) {
					
					actionEvent("alpha");
				}
				if (parent != null) {
					return parent.update(event);
				}
				return false;
			}
			
			@Override
			public void setParent(PsUpdateIf parent) {
				this.parent = parent;
			}
			
			@Override
			public String getName() {
				return AlphaControler.class.getSimpleName();
			}
			
			@Override
			public PsUpdateIf getFather() {
				return parent;
			}
		});
		setLineStatus(false);
		
	}
	
	
	private void actionEvent(String s){
		ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, s);
		for (ActionListener listener : actionListeners) {
			listener.actionPerformed(ae);
		}
	}
	
	
	private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
	public boolean addActionListener(ActionListener listener){
		return actionListeners.add(listener);
	}
	
	public boolean removeActionListener(Object listener){
		return actionListeners.remove(listener);
	}

	

}
